package com.duleme.dao;

import com.duleme.common.DBUtils;
import com.duleme.entity.Delivery;

import java.net.URLDecoder;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class DeliveryDao {
    public Delivery getDeliveryByID(int deliveryID){
        String sql = "SELECT * FROM delivery WHERE deliveryID = ?";
        try {
            Connection connection = DBUtils.getConnection();
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setInt(1,deliveryID);
            ResultSet rs = statement.executeQuery();
            Delivery delivery = new Delivery();
            while (rs.next()){
                delivery.setDeliveryId(rs.getInt("deliveryId"));
                delivery.setCity(rs.getString("city"));
                delivery.setCountry(rs.getString("country"));
                delivery.setCounty(rs.getString("county"));
                delivery.setDetailedAddress(rs.getString("detailedAddress"));
                delivery.setName(rs.getString("name"));
                delivery.setPhoneNumber(rs.getString("phoneNumber"));
                delivery.setProvince(rs.getString("province"));
                delivery.setStreet(rs.getString("street"));
                delivery.setUserId(rs.getInt("userId"));
            }
            DBUtils.close(connection,statement,rs);
            return delivery;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public List<Delivery> getDeliveryByUserID(int userid){
        String sql = "SELECT * FROM delivery WHERE userId = ?";
        try {
            Connection connection = DBUtils.getConnection();
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setInt(1,userid);
            ResultSet rs = statement.executeQuery();
            List<Delivery> list = new ArrayList<>();
            while (rs.next()){
                Delivery delivery = new Delivery();
                delivery.setDeliveryId(rs.getInt("deliveryId"));
                delivery.setCity(rs.getString("city"));
                delivery.setCountry(rs.getString("country"));
                delivery.setCounty(rs.getString("county"));
                delivery.setDetailedAddress(rs.getString("detailedAddress"));
                delivery.setName(rs.getString("name"));
                delivery.setPhoneNumber(rs.getString("phoneNumber"));
                delivery.setProvince(rs.getString("province"));
                delivery.setStreet(rs.getString("street"));
                delivery.setUserId(rs.getInt("userId"));
                list.add(delivery);
            }
            DBUtils.close(connection,statement,rs);
            return list;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public boolean insert(Delivery delivery,int userId){
        String sql="INSERT INTO delivery(userId, country, province, city, county, street, detailedAddress, phoneNumber, name) VALUES (?,?,?,?,?,?,?,?,?)";
        try{
            Connection connection=DBUtils.getConnection();
            PreparedStatement ps=connection.prepareStatement(sql);
            ps.setInt(1,userId);
            ps.setString(2, URLDecoder.decode(delivery.getCountry(),"utf-8"));
            ps.setString(3,URLDecoder.decode(delivery.getCountry(),"utf-8"));
            ps.setString(4,URLDecoder.decode(delivery.getCity(),"utf-8"));
            ps.setString(5,URLDecoder.decode(delivery.getCounty(),"utf-8"));
            ps.setString(6,URLDecoder.decode(delivery.getStreet(),"utf-8"));
            ps.setString(7,URLDecoder.decode(delivery.getDetailedAddress(),"utf-8"));
            ps.setString(8,URLDecoder.decode(delivery.getPhoneNumber(),"utf-8"));
            ps.setString(9,URLDecoder.decode(delivery.getName(),"utf-8"));
            if (ps.executeUpdate()>0){
                DBUtils.close(connection,ps,null);
                return true;
            }
            DBUtils.close(connection,ps,null);
            return false;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    public static  void main(String[] args){
        DeliveryDao deliveryDao = new DeliveryDao();
        Delivery delivery = deliveryDao.getDeliveryByID(2);
        DBUtils.object2Json(delivery);
    }
}
